import asyncio

import aiofiles
import aiohttp

urls = [
    "https://i0.hdslb.com/bfs/archive/deb76808bc40d7ffb26191433830b57e070cbe63.jpg@672w_378h_1c_!web-home-common-cover.avif",
    "https://i0.hdslb.com/bfs/archive/6d4654b1821b0974cd54280250ca805a223d834a.jpg@672w_378h_1c_!web-home-common-cover.avif",
    "https://i2.hdslb.com/bfs/archive/dc2d467a935fa73961d3e45b3f63d886356662a5.jpg@672w_378h_1c_!web-home-common-cover.avif"
]

async def aiodownload(url):
    name = url.rsplit("/",1)[1]
    # 创建aiohttp客户端会话
    async with aiohttp.ClientSession() as session:
        # 发送异步get请求
        async with session.get(url) as resp:
            # 确保响应状态码是200（OK）
            if resp.status == 200:
                async with aiofiles.open(name, mode="wb") as f:
                    await f.write(await resp.content.read())
            else:
                print(f"Failed to download file: Status code {resp.status}")

async def main():
    tasks = []
    for url in urls:
        tasks.append(asyncio.create_task(aiodownload(url)))
    await asyncio.wait(tasks)

if __name__ == '__main__':
    asyncio.run(main())